package cn.yuanqiao.archive.xiaoxitongzhi.service.impl;

import cn.yuanqiao.archive.xiaoxitongzhi.domain.YqXxtz;
import cn.yuanqiao.archive.xiaoxitongzhi.mapper.YqXxtzMapper;
import cn.yuanqiao.archive.xiaoxitongzhi.service.IYqXxtzService;
import cn.yuanqiao.common.service.RedisService;
import cn.yuanqiao.common.utils.DateUtils;
import cn.yuanqiao.common.utils.SecurityUtils;
import cn.yuanqiao.common.utils.SnowflakeIdGenerator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 消息通知Service业务层处理
 * 
 * @author sy
 * @date 2023-10-10
 */
@Service
public class YqXxtzServiceImpl implements IYqXxtzService
{
    @Autowired
    private YqXxtzMapper yqXxtzMapper;

    @Autowired
    private RedisService redisService;

    /**
     * 查询消息通知
     *
     * @param id 消息通知主键
     * @return 消息通知
     */
    @Override
    public YqXxtz selectYqXxtzById(Long id) {
        return yqXxtzMapper.selectYqXxtzById(id);
    }

    @Override
    public YqXxtz selectYqXxtzByRecordId(String id) {
        return yqXxtzMapper.selectYqXxtzByRecordId(id);
    }

    /**
     * 查询消息通知列表
     *
     * @param yqXxtz 消息通知
     * @return 消息通知
     */
    @Override
    public List<YqXxtz> selectYqXxtzList(YqXxtz yqXxtz) {
        yqXxtz.setRecipientsId(SecurityUtils.getLoginUser().getUserId());
        return yqXxtzMapper.selectYqXxtzList(yqXxtz);
    }

    @Override
    public int selectYqXxtzCount() {
        boolean b = false;
        String userid;
        if(SecurityUtils.getLoginUser()==null){
           return 0;
        }else{
            userid = String.valueOf(SecurityUtils.getLoginUser().getUserId());
        }
        String userCode = SecurityUtils.getTenantCode();
        List<String> maps = new ArrayList<>();
        if (!redisService.hasKey(userCode+"message_unread")) {
            maps = yqXxtzMapper.selectYqXxtzCount(userCode);
            if (maps == null || maps.size() < 1) {
                maps.add("1");
            }
            redisService.setCacheList(userCode+"message_unread", maps);
        } else {
            maps = redisService.getCacheList(userCode+"message_unread");
        }
        if (maps != null && maps.size() > 0) {
            b = maps.stream().anyMatch(a -> a.equals(userid));
        }
        if (b) {
            return 1;
        }
        return 0;
    }

    /**
     * 查询消息通知列表
     *
     * @param yqXxtz 消息通知
     * @return 消息通知
     */
    @Override
    public List<YqXxtz> selectYqXxtzListByRemark(YqXxtz yqXxtz) {
        return yqXxtzMapper.selectYqXxtzListByRemark(yqXxtz);
    }

    /**
     * 新增消息通知
     *
     * @param yqXxtz 消息通知
     * @return 结果
     */
    @Override
    public int insertYqXxtz(YqXxtz yqXxtz) {
        yqXxtz.setCreateTime(DateUtils.getNowDate());
        return yqXxtzMapper.insertYqXxtz(yqXxtz);
    }


    /**
     * 新增消息通知
     *
     * @param yqXxtz 消息通知
     * @return 结果
     */
    @Override
    public int insertYqXxtzAndYqXxfbfw(YqXxtz yqXxtz) {
        yqXxtzMapper.addYqXxtz(yqXxtz);
        yqXxtzMapper.addYqXxfbfw(yqXxtz);
        updateMessages(SecurityUtils.getTenantCode());
        return 1;
    }

    /**
     * 新增消息通知
     *
     * @param yqXxtz 消息通知
     * @return 结果
     */
    @Override
    public int insertYqXxtzAndYqXxfbfw(YqXxtz yqXxtz,String tenantCode) {
        yqXxtzMapper.addYqXxtz(yqXxtz);
        yqXxtzMapper.addYqXxfbfw(yqXxtz);
        updateMessages(tenantCode);
        return 1;
    }

    /**
     * 修改消息通知
     *
     * @param yqXxtz 消息通知
     * @return 结果
     */
    @Override
    public int updateYqXxtz(YqXxtz yqXxtz) {
        yqXxtzMapper.updateYqXxtz(yqXxtz);
        yqXxtzMapper.updateYqXxfbfw(yqXxtz);
        updateMessages(SecurityUtils.getTenantCode());
        return 1;
//        return yqXxtzMapper.updateYqXxtz(yqXxtz);
    }

    /**
     * 批量设置已读
     *
     * @param ids 需要设置已读的消息通知主键
     * @return 结果
     */
    @Override
    public int readYqXxtzByIds(Long[] ids) {
        YqXxtz yqXxtz = new YqXxtz();
        int i = yqXxtzMapper.readYqXxtzByIds(yqXxtz.getGlTableName(), ids, SecurityUtils.getLoginUser().getUserId());
        updateMessages(SecurityUtils.getTenantCode());
        return i;
//        if (!(ids[0] == -1)){
//            return yqXxtzMapper.readYqXxtzByIds(yqXxtz.getGlTableName(),ids,SecurityUtils.getLoginUser().getUserId());
//        }else {
//            yqXxtz.setRecipientsId(SecurityUtils.getLoginUser().getUserId());
//            return yqXxtzMapper.readYqXxtzAll(yqXxtz);
//        }
    }

    /**
     * 全部设置已读
     *
     * @return 结果
     */
    @Override
    public int readYqXxtzAll() {
        YqXxtz yqXxtz = new YqXxtz();
        yqXxtz.setRecipientsId(SecurityUtils.getLoginUser().getUserId());
        int i = yqXxtzMapper.readYqXxtzAll(yqXxtz);
        updateMessages(SecurityUtils.getTenantCode());
        return i;
    }

    /**
     * 批量删除消息通知
     *
     * @param ids 需要删除的消息通知主键
     * @return 结果
     */
    @Override
    public int deleteYqXxtzByIds(Long[] ids) {
        YqXxtz yqXxtz = new YqXxtz();
        return yqXxtzMapper.deleteYqXxtzByIds(yqXxtz.getGlTableName(), yqXxtz.getTableName(), ids, SecurityUtils.getLoginUser().getUserId());

//        if (!(ids[0] == -1)){
//            return yqXxtzMapper.deleteYqXxtzByIds(yqXxtz.getGlTableName(), yqXxtz.getTableName(), ids,SecurityUtils.getLoginUser().getUserId());
//        }else {
//            yqXxtz.setRecipientsId(SecurityUtils.getLoginUser().getUserId());
//            return yqXxtzMapper.deleteYqXxtzAll(yqXxtz);
//        }
    }

    /**
     * 批量删除消息通知
     *
     * @return 结果
     */
    @Override
    public int deleteYqXxtzAll() {
        YqXxtz yqXxtz = new YqXxtz();
        yqXxtz.setRecipientsId(SecurityUtils.getLoginUser().getUserId());
        return yqXxtzMapper.deleteYqXxtzAll(yqXxtz);
    }

    /**
     * 删除消息通知信息
     *
     * @param id 消息通知主键
     * @return 结果
     */
    @Override
    public int deleteYqXxtzById(Long id) {
        return yqXxtzMapper.deleteYqXxtzById(id);
    }

    @Override
    public void addyqXxtzApi(String msg, Long publisherId, Long recipientsId, String tenantCode) {
        Long id = SnowflakeIdGenerator.generateId();
        YqXxtz yqXxtz = new YqXxtz(tenantCode);
        yqXxtz.setMsg(msg);
        yqXxtz.setId(id);
        yqXxtz.setType(6);
        yqXxtz.setRecordId(id);
        yqXxtz.setIsRead(0);
        yqXxtz.setRecipientsId(recipientsId);
        yqXxtz.setPublisherId(publisherId);
        yqXxtzMapper.addYqXxtz(yqXxtz);
        yqXxtzMapper.addYqXxfbfw(yqXxtz);
        updateMessages(tenantCode);
    }

    private void updateMessages(String tenantCode) {
        List<String> maps = yqXxtzMapper.selectYqXxtzCount(tenantCode);
        if (redisService.hasKey(tenantCode+"message_unread")) {
            redisService.deleteObject(tenantCode+"message_unread");
        }
        if (maps == null || maps.size() < 1) {
            maps.add("1");
        }
        redisService.setCacheList(tenantCode+"message_unread", maps);
    }
}
